So I want to write a funny little discord bot that's in a russian theme. I want to make it play the USSR Anthem no matter what song is requested, in the channel that's written after "&channel" in the command on discord.
This is the console feed I get, when I type "vadim play something &channel Just Chatting blyat:
Privet
Just Chatting
Ignoring exception in on_menter code hereessage
Traceback (most recent call last):
File "C:\Users\maria\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\maria\Desktop\Coding\Projekte\Python\SovjetBot\Code\bot.py", line 33, in on_message
voicechannel = channel.connect()
AttributeError: 'NoneType' object has no attribute 'connect'
Here's my code:
import discord
from discord.utils import get
import time
class MyClient(discord.Client):
#Triggered on login
async def on_ready(self):
print("Privet")
#Triggered on messages
async def on_message(self, message):
if message.author == client.user:
return
if message.content.lower().startswith("vadim") and message.content.lower().endswith("blyat"):
messagearray = message.content.split(" ")
messagearray.pop()
messagearray.remove("vadim")
if messagearray[0].lower() == "play" and ((len(messagearray)) != 1 or messagearray.contains('&channel')):
where = ""
for i in range(messagearray.index("&channel") + 1, len(messagearray)):
where = where + ' ' + messagearray[i]
where = where[0:]
time.sleep(0.25)
print(where)
channel = get(message.guild.channels, name=where)
time.sleep(0.25)
voicechannel = channel.connect()
time.sleep(0.25)
voicechannel.play(discord.FFmpegPCMAudio('National Anthem of USSR.mp3'))
client = MyClient()
client.run(The bots client id)
TLDR
Change
channel = get(message.guild.channels, name=where)
To
channel = await get(message.guild.channels, name=where)
Explanation
There error indicates you have an issue on the line voicechannel = channel.connect() because channel is NoneType. You define channel on the line above with channel = get(message.guild.channels, name=where). However, according to the documentation the get() function you're using is a coroutine so you need to await it.
Side Note
After you create channel, you should check that it's not None before you try to use it with voicechannel = channel.connect().
Related
I am trying to make a script that adds an emoji to the most recently sent message in a specific text channel in a discord server using an actual discord account rather than a bot.
When I execute my code I get this error message:
Ignoring exception in on_ready
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "C:\Users\plsno\Pictures\mudaebot\main.py", line 10, in on_ready
await channel.add_reaction(emoji)
AttributeError: 'TextChannel' object has no attribute 'add_reaction'
I have been looking up on how to fix this but it has been very hard for me to wrap my head around this since I am pretty new to coding.
The script:
client = discord.Client()
token = ("token")
def add_reaction(emoji):
#client.event
async def on_ready():
channel = client.get_channel(825437474871312387)
await message.add_reaction(emoji)
print("done")
return ""
client.run(token, bot=False)
print("can you see me?")
if __name__ == '__main__':
add_reaction('❤️')
There are two options to handle emojis in Python.
To decode it to a string. (e.g. 😆 = \U0001F606 - You can print each emoji to get it string. Every emoji has a Unicode associated with it).
To use Emoji
Change your code to this:
client = discord.Client()
token = ("token")
#client.event
async def on_message(message):
channel = client.get_channel(825437474871312387)
if message.channel == channel:
await message.add_reaction(emoji)
print("done")
if __name__ == '__main__':
client.run(token, bot=False)
print("can you see me?")
What you’re doing is your getting a channel and then trying to add a reaction to it. Reactions can only be added to messages. Ive changed the code so it adds reactions to every message on that channel
I'm trying to make a discord bot to automate playing my friend's bot's game. When my friend's bot sends a message replying to your message after you use a specific command. I want to be able to check if the message the bot sends is replying to my message specifically. This is what I've got so far:
#bot.event
async def on_message(message):
if message.author.id == botId:
content = str(message.content)
reference = message.reference
if reference.message_id.author.id == myId:
word = content.split('`')[1].split('`')[0]
await bot.send_message(message.channel, word)
I know reference.message_id.author.id will not work since author.id is incompatible with reference.message_id, but I cannot find something that will work with it.
The Error:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\user\OneDrive\Desktop\User\Projects\Programming\Python\Discord Bot\Waterhole Dryer\bot.py", line 30, in on_message
if reference.message_id.author.id == myId:
AttributeError: 'int' object has no attribute 'author'
Thanks!
Alrighty, so... I found a bit of a workaround to this...
#bot.event
async def on_message(message):
if message.author.id == botId:
guild = bot.get_guild(guildId)
channel = guild.get_channel(message.reference.channel_id)
msg = await channel.fetch_message(message.reference.message_id)
if msg.author.id == myId:
word = content.split('`')[1].split('`')[0]
await channel.send(word)
Basically, I made a new message object. I hope this helps anyone else with my problem.
My bot checks whenever a user is added to a guild on Discord and then privately DMs them for their email address. It then sends a one-time code to the email address and asks the user to enter the code in the DM. All this is implemented and works. However, when the user answers with the code, I cannot seems to be able to assign a new role to the user. Here is what I currently have (I removed the code that checks the one-time code, etc. as it works and does not seem to be the source of the problem):
import discord
from discord.ext import commands
from discord.utils import get
#client.event
async def on_message(message):
# Check if message was sent by the bot
if message.author == client.user:
return
# Check if the message was a DM
if message.channel.type != discord.ChannelType.private:
return
user_code = 'some code sent via email'
if message.content == user_code:
member = message.author
new_guild = client.get_guild(int(GUILD_ID))
role = get(new_guild.roles, id=DISCORD_ROLE)
await member.add_roles(role)
response = "You can now use the Discord Server."
await message.channel.send(response)
Here is the error I receive:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "main.py", line 89, in on_message
await member.add_roles(role)
AttributeError: 'User' object has no attribute 'add_roles'
For that, you need to transform the User object into a Member object. That way, you can call the add_roles method. Here is one way of doing it:
import discord
from discord.ext import commands
from discord.utils import get
#client.event
async def on_message(message):
# Check if message was sent by the bot
if message.author == client.user:
return
# Check if the message was a DM
if message.channel.type != discord.ChannelType.private:
return
user_code = "some code sent via email"
if message.content == user_code:
new_guild = client.get_guild(int(GUILD_ID))
member = new_guild.get_member(message.author.id)
role = new_guild.get_role(int(DISCORD_ROLE))
await member.add_roles(role)
response = "You can now use the Discord Server."
await message.channel.send(response)
I'm trying to create a bot that will send a random gif (from the few options I've chosen) upon command.
However, it always comes back as errors, saying that the command wasn't found, even tho I typed in ?hug & attribute errors:
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "hug" is not found
Ignoring exception in command hug:
Ignoring exception in command hug:
Traceback (most recent call last):
File "C:\Users\lemaia\Pictures\Discord\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\lemaia\Documents\DiscordBot\!givebot\v4.0.0.py", line 16, in hug
embed.random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif ', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135'])
AttributeError: 'Embed' object has no attribute 'random'"
embed.random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif ', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135'])
AttributeError: 'Embed' object has no attribute 'random'"
import discord
from discord.ext import commands
import random
client = commands.Bot(command_prefix='?')
#client.event
async def on_ready():
print("Bot is ready for use <3")
print(client.user.name)
print('------------------------')
#client.command()
async def hug(ctx):
embed = discord.Embed(title = 'A hug has been sent!', description = 'warm, fuzzy and comforting <3', color = 0x83B5E3)
embed.random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif ', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135'])
await ctx.channel.send(embed=embed)
client.run('TOKEN')
You trying to use attribute random of Embed object, not call random.choice.
To set image in embed you need to use discord.Embed.set_image:
import discord
from discord.ext import commands
import random
client = commands.Bot(command_prefix='?')
#client.event
async def on_ready():
print("Bot is ready for use <3")
print(client.user.name)
print('------------------------')
#client.command()
async def hug(ctx):
image = random.choice(['https://media.discordapp.net/attachments/414964961953972235/570600544226508821/Server_Welcome.gif', 'https://media.giphy.com/media/l4FGpP4lxGGgK5CBW/giphy.gif', 'https://media.giphy.com/media/fvN5KrNcKKUyX7hNIA/giphy.gif', 'https://tenor.com/view/milk-and-mocha-cuddling-hug-cute-kawaii-gif-12535135']
embed = discord.Embed(title = 'A hug has been sent!', description = 'warm, fuzzy and comforting <3', color = 0x83B5E3)
embed.set_image(url=image))
await ctx.send(embed=embed)
client.run('TOKEN')
Context objects has an send attribute, which is basically alias to Context.channel.send
I am trying to create a function that would clear the chat on discord using https://www.youtube.com/watch?v=ZBVaH6nToyM , however I think that I've declared channel (channel = ctx.message.channel) however entering .clear would result in an error
Ignoring exception in command clear
Traceback (most recent call last):
File "C:\Users\mark\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\discord\ext\commands\core.py", line 50, in
wrapped
ret = yield from coro(*args, **kwargs)
File "C:/Users/mark/Desktop/purge.py", line 17, in clear
await client.delete_message(messages)
File "C:\Users\mark\AppData\Local\Programs\Python\Python36-
32\lib\site-packages\discord\client.py", line 1261, in delete_message
channel = message.channel
AttributeError: 'list' object has no attribute 'channel
import discord
from discord.ext import commands
from discord.ext.commands import Bot
TOKEN = "token here"
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Bot is online")
#client.command(pass_context = True)
async def clear(ctx, amount=100):
channel = ctx.message.channel
messages = []
async for message in client.logs_from(channel,limit=int(amount)):
messages.append(messages)
await client.delete_message(messages)
await client.say('message has been deleted')
client.run(TOKEN)
You are basically appending a list itself over and over again
my_list=[]
my_list.append(my_list)
print(my_list)
Which results in something like this
>>[[[...]]
The command should be something like this
#bot.command(pass_context=True)
async def clear(msg,amt:int=20): #can be converted into int via :type
messages=[]
async for i in bot.logs_from(msg.message.channel,limit=amt):
messages.append(i)
await bot.delete_messages(messages)
Also a heads up note, you can't bulk delete messages that are older than 14 days I think meaning that you can't use the delete_messages and only delete it individually