AttributeError: 'str' object has no attribute 'channel' is the error I am getting. I have been looking around however, the answer still isn't clear. The code was working before, but now it is not. I don't think I made any changes to channel attribute.
client = discord.Client()
#client.event
async def on_message(message):
if 'SJAY' in u:
message = '{0.author.mention}, Did you know Sjay is a God?'.format(message)
await client.send_message(message.channel, message)
client.loop.create_task(change_stat())
client.run(TOKEN)
That's because before using it you changed its value into a string. Doing the following changes will resolve the issue:
client = discord.Client()
#client.event
async def on_message(message):
if 'SJAY' in u:
msg = '{0.author.mention}, Did you know Sjay is a God?'.format(message)
await client.send_message(message.channel, msg)
client.loop.create_task(change_stat())
client.run(TOKEN)
Related
I have a function that checks if 'your' is changed to 'you're' and I am having trouble having the bot send a message after this check. What is the proper way to do this? (I want this to apply to all channels.)
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix='$')
#client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
#client.event
async def on_message_edit(before,after):
if(before.content == "your" and after.content == "you're"):
# I want to send message here
client.run('Token') # I have my token here
Add ctx into the parameters (before,after,ctx), and
then put in await ctx.send("message") where you want your message to send.
the parameters before and after are of type discord.Message. discord.Message has an attribute channel, with which you can use the function send.
#client.event
async def on_message_edit(before, after):
if before.content == 'your' and after.content == "you're":
await before.channel.send(send_whatever_you_want)
If someone mentions me how do I get the bot to read that? like if someone mentions me I want the bot to say something back. How would I do that?
#client.event
me = '<#user_id>'
async def on_message(message):
member = message.author.id
if message.content == me:
await message.channel.send('my master will be back shortly')
else:
return
await client.process_commands(message)
You can use discord.Message.mentions. It returns a list of discord.Member that were mentioned in message.
#client.event
async def on_message(message):
me = message.guild.get_member(<user id>)
if me in message.mentions:
await message.channel.send('my master will be back shortly')
await client.process_commands(message)
Reference
discord.Message.mentions
so I am trying to make the bot do something when the premium_guild_subscription message comes. I have tried:
#bot.event
async def on_message(message):
MessageType = discord.MessageType
if MessageType == 'pins_add':
await message.channel.send('ITS WORKING!!!')
but its not working. The above code does nothing when the message comes. If I do message.MessageType then it says that message object has no attribute MessageType.
You need to use an instance of a class and compare that to see if it's the correct type, not comparing the class itself to a string.
#bot.event
async def on_message(message):
if message.type == discord.MessageType.pins_add:
await message.channel.send('ITS WORKING!!!')
A further example:
#bot.command()
async def test(ctx):
print(ctx.message.type == discord.MessageType.pins_add)
print(ctx.message.type == discord.MessageType.default)
Results in
!test
> False
> True
In your code, you're trying to use class. You have to call the init function from that class. So you need to access a MessageType type data from message.
According to API References, you can use message.type to access MessageType object. Then you can check if a message is pins_add type with message.type.pins_add.
#bot.event
async def on_message(message):
if message.type == discord.MessageType.pins_add:
await message.channel.send('ITS WORKING!!!')
I am making a bot that mutes people when they join a vc. However, when I run the following code I receive an error about 'self' not being defined. How would I fix this?
async def join(ctx, *, member=discord.Member):
channel = ctx.author.voice.channel
await channel.connect()
await member.edit(mute=True)
If you're inside a Class/Cog you need to pass self as the first parameter.
async def join(self, ctx, *, member=discord.Member):
channel = ctx.author.voice.channel
await channel.connect()
await member.edit(mute=True)
I’m trying to make a code that you can dm the bot for help, suggestions, or whatever and it will send the message in a channel. Once you get the message you can do .dm (user) (message) and then the bot will then dm the user back. I’m having some issues I’m pretty new to coding and I think my format is outdated. If someone could help that would be awesome.
#vini.event
async def on_message(message):
channel = vini.get_channel('633053538334932992')
if message.guild is None and message.author != vini.user:
await channel.send(message.content)
await vini.process_commands(message)
#vini.command(pass_context=True)
#commands.is_owner()
async def dm(ctx):
memberID = "ID OF RECIPIENT"
person = await vini.get_user_info(memberID)
await ctx.send(“WHAT TO SAY", delete_after=2)
Error:
File "main.py", line 133, in on_message
await channel.send(message.content)
AttributeError: 'NoneType' object has no attribute 'send'
If someone could help it would be greatly appreciated. Thanks!