will that work? its a bot that responds to messages from a certain user - discord

import discord
from replit import db
from keep_alive import keep_alive
intents = discord.Intents.default()
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
if message.author.id == "667020238340096000":
await message.channel.send("ok naplet")
keep_alive()
client.run(
"token goes here but i deleted it to share this")
will it work? help me

Related

how to insert #client.event in #client.command just new to discord py program i hava a question about this followingcode

is it possiple to insert client.event on client.command
this is my code please explain this issue and add your code for future reference
import discord
from discord.ext import commands
import random
import asyncio
from random import randint
import aiohttp
from discord.ext.commands.core import command
client = commands.Bot(command_prefix = '')
client.remove_command('help')
#client.event
async def on_ready():
print('we are logged in {0.user}'.format(client))
#client.command()
async def say(ctx, *, args):
embed=discord.Embed(title= args, color=0x3700ff)
await ctx.send(embed=embed)
#client.event
async def on_message(message):
if message.content.startswith('stop'):
await message.channel.send('start')
#client.run("my token")
am try to understand why #client.event on_message will not working on #client.command
disclimer:- am noob on code so need a professionals help so thank about the helper

how can I add context menu from app_commands

I want to add context menu from app_commands, I tried that, but it didn't work
import discord
from discord import app_commands
from discord.ext import commands
#app_commands.context_menu(name="Hello")
async def hello(interaction: discord.Interaction, message: discord.Message):
await interaction.response.send_message("Hey !")
I'm using discord.py 2.0.1, how can I add?
Context menu's are a bit tricky to get working, here's an example:
async def hello(interaction: discord.Interaction, message: discord.Message):
await interaction.response.send_message(f"Hey {message.author.mention}!")
hello_context_menu = app_commands.ContextMenu(
name='Say Hello',
callback=hello,
)
bot.tree.add_command(hello_context_menu)

AttributeError: 'Client' object has no attribute 'get_all_emojis'

from distutils.util import change_root
from discord.utils import get
import discord
import random
import shutil
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
channel = client.get_channel(968268255804420096)
emoji = await discord.utils.get(client.get_all_emojis(), name='upvote')
await client.add_reaction(message, emoji)
i'm trying to make a bot react to all messages in a specific channel, but i keep getting the "AttributeError: 'Client' object has no attribute 'get_all_emojis'" error
any help?
This no longer works because get_all_emojis was removed a long time ago. Instead, use client.emojis.
emoji = discord.utils.get(client.emojis, name='upvote')

Discord.py not unpinning own bot's messages

My test code is
#bot.command(name='test')
async def test(ctx):
message = await ctx.send('test')
channel = ctx.channel
message = await channel.fetch_message(message.id)
await message.pin()
await message.unpin()
The bot pins the message but it won't unpin it.
I tested the code, it works fine for me.
NOTE- Discord only shows a message that someone has pinned the message, but discord does not show if someone has unpinned it, you need to check it in the pinned messages.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = '!')
#bot.event
async def on_ready():
print('Logged successfully!')
#bot.command()
async def test(ctx):
message = await ctx.send('test')
await message.pin()
await message.unpin()
bot.run('BOT_TOKEN_HERE')

Could not leave a discord guild

I'm trying to make my bot leave any guilds if the guilds size reaches 2 guilds
I tried everything from the Discord.js docs using leave()
// Here is my code on guildCreate.js event
const Discord = require('discord.js');
const client = new Discord.Client();
let guildArray = client.guilds.array();
module.exports = async function (msg,guild) {
if(guildArray.size > 1)
await guild.leave()
};
It should works and leave the guild because the size is more than 2 guilds but it do nothing .
I think you want something like this if I am right:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
let guildnumber = client.guilds.size;
while(guildnumber > 2 )
{
client.guilds.first().leave();
guildnumber--;
}
});
client.log(token);
You can put it into ready and/or guildAdd as guildAdd only emits when your bot gets added to a guild and is online so you should check if your bot was added during it was offline

Resources