I want to make a Bot command, that adds a reaction to a message when given emoji and ID of the message.
It seems like I need to turn the string I get from the original message into the discord.Message class for the code to work, but I don't find any way of doing that.
I've already read through the documentation and it just says
"There should be no need to create one of these manually."
import discord
token = "Token Here"
client = discord.Client()
#client.event
async def on_message(message):
if message.content.lower().startswith("e react"):
msg_id = message.content[8:26] #This filters out the ID
emoji = message.content[27:] #This filters out the Emoji
await msg_id.add_reaction(emoji)
client.run(token)
I get the following error:
AttributeError: 'str' object has no attribute 'add_reaction'
This is made much easier by using converters. Specifically, we can use MessageConverter to get the target Message object and PartialEmojiConverter to get the emoji we want to react with:
from discord.ext.commands import Bot
from discord import Message, PartialEmoji
bot = Bot("e ")
#bot.command()
async def react(ctx, message: Message, emoji: PartialEmoji):
await message.add_reaction(emoji)
bot.run("TOKEN")
Related
Im trying to make a command for my discord bot that gets the message content from a specific channel without using discord.ext, I have tried putting it inside my async def on_message(message) function and it doesnt want to work. I know discord.ext will be better for this but I still want to what I have been using.
Heres my code:
TOKEN = 'MY_BOTS_TOKEN'
import discord
intents = discord.Intents.all()
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
mychannel = client.get_channel(MYCHANNELID)
content = (mychannel.history(limit=1))[0].content
if message.content.startswith('.content'):
await message.channel.send(content)
client.run(TOKEN)
I tried putting the mychannel = client.get_channel(MYCHANNELID) content = (m7channel.history(limit=1))[0].content into a different function and then calling onto it in message.channel.send(). I wasnt expecting it to work and I guessed correctly.
I also tried putting (await mychannel.history(limit=1))[0].content) inside of message.channel.send() but it returned the same error as the title.
so I'm trying to code a basic discord moderation bot for my server. What I'm attempting to do is have the bot check a message to see if it contains a word from a text file (in the same directory). Here is my code:
import os
from keep_alive import keep_alive
from datetime import datetime
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
for word in bad_words:
if message.content.contains(word):
await message.delete()
badwordEmbed = discord.Embed(title="You have sent a bad word!", description="", colour=0xff0000, timestamp=datetime.utcnow())
await message.channel.send(embed=badwordEmbed)
keep_alive()
client.run(os.getenv('TOKEN'))
However, when I send a discord message containing a word from the .txt file, I get this error:
if message.content.contains(word):
AttributeError: 'str' object has no attribute 'contains'
I have a feeling this is a basic error, but for now I'm not sure so I'd appreciate any help.
It's in you need:
if word in message.content:
Strings have a __contains__ method (note the underscores), but this is used by in.
I'm trying to define a function that will post a message to a specific discord channel when the function is called. I have no idea how to do it. This is what I have so far. Can someone help out?
edit: I need it to run whenever the output() function is called, regardless of other events.
import discord
import asyncio
token = 'BOT TOKEN'
channelid = 'CHANNEL ID'
client = discord.Client()
def respond(output):
#this should make a discord bot send a message with a value of output to the channel of channelid
client.run(token)
respond('hello world')
You can use bot.get_channel(channelid).
This is just an example sorry for the bad formatting I'm not got with formatting in stack-overflow. :
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
channel = bot.get_channel(yourchannelid) #a specific channel
await channel.send('Bot is ready')
I'm attempting to create a Discord bot using Discord.py for a server I'm in, I'm trying to get the bot to mention (blue links) other channels in the server. I thought it would have been using something like discord.TextChannel.name, discord.TextChannel.mention. But neither of those work, they only return something along the lines of "<property object at 0x03CE8B68>".
Code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='$')
client = discord.Client()
token = <token>
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
#bot.command()
async def mentionChan(ctx):
await ctx.send(ctx.guild.get_channel('662129928463974411').mention)
client.run(token)
#botclient.command()
async def mentionChan(ctx):
await ctx.send(ctx.guild.get_channel(CHANNEL_ID).mention)
This will mention the channel with the given id. You can get the id of a channel by right clicking a channel and selecting "Copy ID". This is only possible after you have turned on developer mode at the appearance tab in User Settings.
ctx.guild.text_channels will give you a list of all the text channels in the server if you want to use that.
I am trying to create a command to list all of the invites created by a user (including how many uses each link has) and return a total in chat. But from my current code all I get back is:
<generator object Client.invites_from at 0x7f877ecc5780>
Here is my code to see where I am going wrong:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
bot = discord.Client()
client = commands.Bot(command_prefix = "!")
#client.event
async def on_ready():
print("Bot Connected!")
#client.event
async def on_message(message):
if message.content == "!inv":
server = message.channel.server.id
invites = bot.invites_from(server)
await client.send_message(message.channel, invites)
client.run("TOKEN")
If I try to loop through the returned generator I get the following error:
for i in invites:
File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/discord/client.py", line 2693, in invites_from
data = yield from self.http.invites_from(server.id)
AttributeError: 'str' object has no attribute 'id'
Thanks a lot for any help in advance
Your problem appears to be with this line in on_message(), as pointed out in this comment:
server = message.channel.server.id
The traceback indicates that the discord.py library is trying to use the id attribute of the server you pass it; currently, you're passing it the ID, but you probably need to pass the whole server object.
IOW, do this instead:
server = message.channel.server
Since invites_from is a coroutine, you'll also need to await it:
invites = await bot.invites_from(server)
I have managed to fix it, it was that I had client and bot the wrong way around so the bot couldn't provide the token to the API, Thanks for all the help everyone