Empty message by discord bot - python

i'm trying to make embed and my bot is sending empty message
I have token, i just deleted him from this message.
And with deleted 'help' don't working too.
bot = commands.Bot (command_prefix=config['prefix'], intents=intents, help_command=None)
bot.remove_command('help')
#bot.event
async def on_ready():
print('Bot connected')
#bot.command()
async def info(ctx):
embed = discord.Embed(
title='title',
description='description',
colour=discord.Colour.dark_gold()
)
await ctx.send(embed=embed)
#bot.event
async def on_message(ctx):
if ctx.author != bot.user:
if filter_mata(ctx.content) == True:
await ctx.delete()
print(f'message {ctx.id} by {ctx.author} deleted')
await bot.process_commands(ctx)
bot.run (config['token'])

Related

Python Discord Bot isn't joinig voice channel

import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print("I'm logged in as {0.user}.".format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("!test"):
await message.channel.send("Test passed successfully!")
#commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
voice_channel = client.get_channel(ctx.author.voice.channel.id)
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
await ctx.send("idk")
#commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
The bot doesn't join I don't know what I did wrong. It should only join my voice channel nothing else. I watched this Tutorial but as I said it didn't worked.
You're mixing metaphors a little: your on_message is a plain function:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("!test"):
await message.channel.send("Test passed successfully!")
Whereas your join and disconnect functions look as if they belong in a class (note the self argument):
#commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
voice_channel = client.get_channel(ctx.author.voice.channel.id)
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
await ctx.send("idk")
#commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
If you remove the self argument from those functions, you should be able to get it working:
async def join(ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
voice_channel = client.get_channel(ctx.author.voice.channel.id)
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
await ctx.send("idk")
#commands.command()
async def disconnect(ctx):
await ctx.voice_client.disconnect()

Make discord bot connect to vc with deafen

I use voice = await message.author.voice.channel.connect() to connect my bot to voice channel and i want the bot connect and deafen himself
All my code:
client = discord.Client()
#client.event
async def on_ready():
print("Bot is ready!")
#client.event
async def on_message(message):
arg = message.content.split(' ')
if message.content.startswith('>play'):
voice = await message.author.voice.channel.connect()
voice.play(discord.FFmpegPCMAudio(YouTube(arg[1]).streams.first().download()))
await message.guild.change_voice_state(channel=voice, self_mute=True, self_deaf=False)
client.run('my token')
Use await message.guild.change_voice_state(channel=voice, self_mute=False, self_deaf=True)
Copy-paste this code that I wrote after looking at your comments:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='>')
#bot.event
async def on_ready():
print("Bot is ready!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
await bot.process_commands(message)
#bot.command()
async def join(ctx):
if ctx.author.voice is None or ctx.author.voice.channel is None:
return await ctx.send('You need to be in a voice channel to use this command!')
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
vc = await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
vc = ctx.voice_client
vc.play(discord.FFmpegPCMAudio(YouTube(arg[1]).streams.first().download()))
await ctx.guild.change_voice_state(channel=vc, self_mute=False, self_deaf=True)
bot.run('my token')

on message event make my bot don't working

My commands don't work and I think it's because of the on_message event. When it checks if message is in the good channel, it actually "steel" the command so my bot commands are not triggered but I don't know how to fix that
import discord, os
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='.',intents=intents)
defrole = ""
cmdchannels = ["none"]
#bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
await bot.change_presence(activity=discord.Game('.help'))
#bot.event
async def on_member_join(member):
await member.add_roles(discord.utils.get(member.guild.roles, name=defrole))
#bot.event
async def on_message(message):
if message.channel.id in cmdchannels :
await message.delete()
#bot.command()
async def setcmd(ctx, arg):
global cmdchannels
cmdchannels.append(discord.utils.get(ctx.guild.channels, name=arg).id)
print(cmdchannels)
await ctx.send(arg+' is now defined as a cmd/bot channel !')
#bot.command()
async def defaultrole(ctx):
global defrole
defrole = str(ctx.message.role_mentions[0])
await ctx.send(defrole+' is now the default on join role !')
bot.run("Token")
You will need to process the commands, add this line at the end of your on_message function:
await bot.process_commands(message)
Reference: process_commands()
on_message event blocks your commands from working. In order to prevent this, you have to process commands.
#bot.event
async def on_message(message):
if message.channel.id in cmdchannels :
await message.delete()
await bot.process_commands(message)

discord.py mute everyone in voice channel

this is my code rn:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="-")
#client.event
async def on_ready():
print('BOT ACTIVATED')
#client.command()
async def hello(ctx):
await ctx.send("hei this is a test just dont mind me")
#client.command()
async def join(ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
client.run('mytoken')
how do i mute everyone in that voice channel?
Iterate through all members of the voice channel and pass mute=True to the edit function.
#client.command()
async def vcmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=True)

Check if a message was sent by a user in a DM (discord.py) then send their message from a direct message into a certain channel?

msg_dump_channel = 1234
#bot.event
async def on_message(message: discord.Message):
channel = bot.get_channel(msg_dump_channel)
if str(message.author) == "user":
await channel.send(message.content)
await bot.process_commands(message)
This is my code, I know that DMs don't have a guild, so how would you write it for a DM?
this should work for an example
#bot.event
async def on_message(message)
guild = message.guild
if not guild:
print(" DM: {0.author.name} : {0.content}".format(message))
discord.Message objects have a channel attribute that you can use for your check:
#bot.event
async def on_messsage(message)
if isinstance(message.channel, discord.DMChannel) and str(message.author) == 'user':
channel = bot.get_channel(channel_id)
await channel.send(message.content)
else:
pass
bot.process_commands(message)

Categories