I have a simple issue that I'm yet to figure out, how can I change my bot's own nickname, because I want to display information instead of its nickname continuously.
tried this :
await bot.user.edit(username=price)
but this actually changes the username which is not allowed to be done multiple times.
async def status_update():
while True:
price = str(get_BTC_price(ws))
await bot.user.edit(username=price)
await asyncio.sleep (2)
#bot.event
async def on_ready():
bot.loop.create_task(status_update())
Thanks
Nicknames are tied to Guild Members. In order to change your bot's nickname, you must
Get the Guild Member object for your bot
Use <Member>.edit(nick=nick)
Note that this will only change the nickname for one server. If you wanted it to be global you would have to loop through each guild and perform the operation (expensive). You may want to use the status box instead to convey this information.
Related
So I am trying to get all guilds that the bot is in. Which I have done by:
for guild in self.bot.guilds:
guild = guild
Using this I need to try and ban the member specified in all of the guilds that the bot is in.
Can anyone help with this?
I have already tried by looping the code to ban the user in all of the servers but seems to not ban them with no error in the console.
Assuming that this is being called in an async function and you have gotten the USER_ID of the user you want to ban. You have the pycord label as well - so linked those docs.
user_id = USER_ID # the ID of the 'specified' member you want to ban
async for guild in self.bot.fetch_guilds():
# could also use guild.get_member(user_id) if you Intents.members enabled and member cache
try:
member = await guild.fetch_member(user_id)
except discord.NotFound:
# user wasn't in this guild
# move to next guild
continue
await member.ban()
Using fetch_guilds in case you don't have them in the cache. And fetch_member in case they're not cached either - but you can use get_member instead if you have Intents.members enabled and member caching enabled.
member.ban docs.
How do I make a discord.py bot not react to commands from the bot's DMs? I only want the bot to respond to messages if they are on a specific channel on a specific server.
If you wanted to only respond to messages on a specific channel and you know the name of the channel, you could do this:
channel = discord.utils.get(ctx.guild.channels, name="channel name")
channel_id = channel.id
Then you would check if the id matched the one channel you wanted it to be in. To get a channel or server's id, you need to enable discord developer mode. After than you could just right click on the server or channel and copy the id.
To get a server's id you need to add this piece of code as a command:
#client.command(pass_context=True)
async def getguild(ctx):
id = ctx.message.guild.id # the guild is the server
# do something with the id (print it out)
After you get the server id, you can delete the method.
And to check if a message is sent by a person or a bot, you could do this in the on_message method:
def on_message(self, message):
if (message.author.bot):
# is a bot
pass
You Can Use Simplest And Best Way
#bot.command()
async def check(ctx):
if not isinstance(ctx.channel, discord.channel.DMChannel):
Your Work...
So just to make the bot not respond to DMs, add this code after each command:
if message.guild:
# Message comes from a server.
else:
# Message comes from a DM.
This makes it better to separate DM from server messages. You just now have to move the "await message.channel.send" function.
I assume that you are asking for a bot that only listens to your commands. Well, in that case, you can create a check to see if the message is sent by you or not. It can be done using,
#client.event
async def on_message(message):
if message.author.id == <#your user id>:
await message.channel.send('message detected')
...#your code
Hopefully this isn't a duplicate post but I've searched around and can't find anything on how to do this specifically.
I'm trying to create a Discord bot which can delete messages sent by a specific author, in a specific channel, but also checks previously sent messages.
At the moment I have the below, which adds new messages from chosen user to a list, checks if they're duplicates and if they are, deletes them.
I want to know:
A: How can I make this bot Channel specific
B: Can I have this program check old messages in that channel too and also delete them if they're duplicates?
Thanks in advance for any help and if any further info required, please let me know.
import discord
TOKEN = ('MY_TOKEN_LIVES_HERE')
client = discord.Client()
messagesSeen = []
#client.event
async def on_message(message):
if "News_Bot" in str(message.author):
if message.content in messagesSeen:
await message.delete()
else:
messagesSeen.append(message.content)
client.run(TOKEN)
Yes you can do both of those things. Since you are using on_message event you can just use message.channel to also check that channel for previously sent messages. I would also advise to change news_bot to the ID of the bot. The thing is one you implement this and clear the channel history for those messages, it won't be necessary anymore as the bot will catch the messages as soon as they are sent. So the message history is kind of a one time thing, which you can just create a cmd and run it in that channel.
messagesSeen = []
#client.event
async def on_message(message):
if 1234345 is message.author.id:
if message.content in messagesSeen:
await message.delete()
async for message in message.channel.history(limit=100):
if message.content in messagesSeen:
await message.delete()
else:
messagesSeen.append(message.content)
I am currently trying to send a message to a user's DM but I only get the following error
user = client.get_user(499642019191324692)
AttributeError: module 'discord.client' has no attribute 'get_user'
This is my code
#bot.command()
async def test(ctx):
user = client.get_user(499642019191324692)
await user.send('Hello')
Thx #Astrogeek for your answer, even if only covering part of the issue. I got a similar issue, and this "bot" instead of client helped me A LOT.
To take the example back :
#bot.command()
async def test(ctx):
user = bot.get_user(499642019191324692)
await user.send('Hello')
Should instead be :
#bot.command()
async def test(ctx):
user = await bot.fetch_user(499642019191324692)
await user.send('Hello')
await fetch_user(user_id)
This function is a coroutine.
Retrieves a User based on their ID. This can only be used by bot accounts. You do not have to share any guilds with the user to get this information, however many operations do require that you do.
(from DiscordPy API Reference)
Since it's a coroutine, you have to "await" it. If you don't, the coroutine hasn't time to get the user from its ID and it results in a "can't send non-None value to a just-started coroutine" with bot.fetch_user(id)
As said in API you can get user from ID that way, but it only guarantees to deliver the message if bot and user are in a same guild or are friends.
you've used bot.command so it'll be bot.get_user instead of client.get_user
#bot.command()
async def test(ctx):
user = bot.get_user(499642019191324692)
await user.send('Hello')
I made recently a discord bot for small server with friends. It is designed to answer when mentioned, depending on user asking. But the problem is, when someone mention bot from a phone app, bot is just not responding. What could be the problem?
Code:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = '=')
reaction = "🤡"
#bot.event
async def on_ready():
print('Bot is ready.')
#bot.listen()
async def on_message(message):
if str(message.author) in ["USER#ID"]:
await message.add_reaction(emoji=reaction)
#bot.listen()
async def on_message(message):
mention = f'<#!{BOT-DEV-ID}>'
if mention in message.content:
if str(message.author) in ["user1#id"]:
await message.channel.send("Answer1")
else:
await message.channel.send("Answer2")
bot.run("TOKEN")
One thing to keep in mind is that if you have multiple functions with the same name, it will only ever call on the last one. In your case, you have two on_message functions. The use of listeners is right, you just need to tell it what to listen for, and call the function something else. As your code is now, it would never add "🤡" since that function is defined first and overwritten when bot reaches the 2nd on_message function.
The message object contains a lot of information that we can use. Link to docs
message.mentions gives a list of all users that have been mentioned in the message.
#bot.listen("on_message") # Call the function something else, but make it listen to "on_message" events
async def function1(message):
reaction = "🤡"
if str(message.author.id) in ["user_id"]:
await message.add_reaction(emoji=reaction)
#bot.listen("on_message")
async def function2(message):
if bot.user in message.mentions: # Checks if bot is in list of mentioned users
if str(message.author.id) in ["user_id"]: # message.author != message.author.id
await message.channel.send("Answer1")
else:
await message.channel.send("Answer2")
If you don't want the bot to react if multiple users are mentioned, you can add this first:
if len(message.mentions)==1:
A good tip during debugging is to use print() So that you can see in the terminal what your bot is actually working with.
if you print(message.author) you will see username#discriminator, not user_id