I have this command called p!slap and I want it so that my bot will say {message.author} slapped {message.mention} and a random gif but I have no idea how to. Can anyone help? Thanks!
#client.command()
async def slap(ctx, message.mention):
embedVar = discord.embed(title=f"<#{message.author.id}> slapped {message.mention}")
list = [
#gif links and stuff
]
await ctx.channel.send(random.choice(list))```
Your code has a few problems.
At first, I couldn't understand what you're trying to do with naming a parameter message.mention but I guess what you're trying to do is "slapping" the person you mentioned in command. For that, you have to get the member object in parameter. Then, you can mention this member.
Also, you shouldn't define a variable named list. This might cause a error because of the built-in method list.
An another thing is, there's no embed method for discord module, it must be discord.Embed. You have to pay attention to the uppercase letters.
You never sent the embed you defined, you must send it for your command to work.
For the last one, I don't know what'll be in the list lst but I guess there will be files. If you're going to send a file, you cannot just send it. You have to pass a parameter in .send() method. But this is only for the files. If you're just going to send a link, then you don't have to pass any parameters.
#client.command()
async def slap(ctx, member: discord.Member):
embedVar = discord.Embed(title=f"{ctx.author.mention} slapped {member.mention}")
lst = [
#gif links and stuff
]
await ctx.channel.send(file=discord.File(random.choice(lst)), embed=embedVar)
Related
If I want to remove an ID-specified role within a function, how can I do that?
#bot.command()
async def aa(ctx,member:discord.Member):
role_muted = discord.utils.get(ctx.guild.roles, name='test01')
await member.remove_roles(role_muted)
What I want is to specify an ID in a function like this.
#bot.command()
async def aa(ctx):
member = 414087251992117211 #ID User
role_muted = discord.utils.get(ctx.guild.roles, name='test01')
await member.remove_roles(role_muted)
how can i do it The reason I have to do this is because I will continue to develop such as When you receive this Role within 3 days if not online it will remove your Role.
Use Guild.get_member(id) to get a member by their ID. You can get the Guild either from the ctx if you want it to run in the guild where you call the command, or from a Guild ID that you store somewhere using Bot.get_guild(id).
If you want to pass the ID in as an argument to the function and have it convert to a member automatically, use a Converter.
Can you give an example? I tried to do as you said. but an error occurred
#bot.command()
async def aa(ctx):
member = discord.Guild.get_member(414087251992117211)
role_muted = discord.utils.get(ctx.guild.roles, name='test01')
await member.remove_roles(role_muted)
Error
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'remove_roles'
#UpFeR Pattamin, please comment on a reply instead of posting a new one. You're getting this error because you're not specifying which guild you are talking about. If you want to get the one that the command was executed in, I'm pretty sure you need to do ctx.guild.get_member().
I got the problem, that when I use the purge command in my Bot using discord.py rewriting method, it doesn't work. What I mean by that is that when I run the code, and then write 'clear' in the discord channel, it simply doesn't deletes the given amount of messages, and it also doesn't raises an error. I've also tried to put print('test') in the definition, but then it only prints test...
That is the Code I used to do this:
#client.command
async def clear(ctx, amount=5):
await ctx.channel.purge(limit=amount)
the proper use of #client.command is #client.command(). See if that fixes it.
I'm relatively new to discord.js, and I've started building a bot project that allows a user to create a message via command, have that message stored in a hidden channel on my private server, and then said message can be extracted through the message ID.
I have the write working and it returns the message ID of the message sent in the hidden channel, but I'm completely stumped on the get command. I've tried searching around online but every method I tried would return errors like "Cannot read property 'fetch' of undefined" or "'channel' is not defined". Here are some examples of what I tried, any help would be appreciated. Note that my args is already accurate, and "args[0]" is the first argument after the command. "COMMAND_CHANNEL" is the channel where the command is being executed while "MESSAGE_DATABASE" is the channel where the targeted message is stored.
let msgValue = channel.messages.cache.get(args[0])
client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content)
let msgValue = msg.channel.message.fetch(args[0])
.then(message => client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content))
.catch(console.error);
I even tried using node-fetch to call the discord API itself
const api = require("node-fetch")
let msgValue = api(`https://discordapp.com/api/v8/channels/${MESSAGE_DATABASE}/messages/${args[0]}`)
.then(message => client.channels.cache.get(COMMAND_CHANNEL).send(msgValue.content))
.catch(console.error);
Am I missing something or am I making some sort of mistake?
Edit: Thanks for the help! I finished my bot, it's just a little experimental bot that allows you to create secret messages that can only be viewed through their ID upon executing the command :get_secret_message <message_id>. I posted it on top.gg but it hasn't been approved yet, so in the meantime if anyone wants to mess around with it here is the link: https://discord.com/api/oauth2/authorize?client_id=800368784484466698&permissions=76800&scope=bot
List of commands:
:write_secret_message - Write a secret message, upon execution the bot will DM you the message ID.
:get_secret_message <message_id> - Get a secret message by its ID, upon execution the bot will DM you the message content.
:invite - Get the bot invite link.
NOTE: Your DMs must be turned on or the bot won't be able to DM any of the info.
My test message ID: 800372849155637290
fetch returns the result as promise so you need to use the then to access that value instead of assigning it to a variable (msgValue). Also you made a typo (channel.message -> channel.messages).
I would recommend using something like this:
msg.channel.messages
.fetch(args[0])
.then(message => {
client.channels
.fetch(COMMAND_CHANNEL)
.then(channel => channel.send(message.content))
.catch(console.error)
})
.catch(console.error)
I think you were quite close with the second attempt you posted, but you made one typo and the way you store the fetched message is off.
The typo is you wrote msg.channel.message.fetch(args[0]) but it should be msg.channel.messages.fetch(args[0]) (the typo being the missing s after message). See the messages property of a TextChannel.
Secondly, but this is only a guess really as I can't be sure since you didn't provide much of your code, when you try to fetch the message, you are doing so from the wrong channel. You are trying to fetch the message with a given ID from in the channel the command was executed from (the msg.channel). Unless this command was executed from the "MESSAGE_DATABASE" channel, you would need to fetch the message by ID from the "MESSAGE_DATABASE" channel instead of the msg.channel.
Thirdly, if you fetch a message, the response from the Promise can be used in the .then method. You tried to assign the response to a variable msgValue with let msgValue = msg.channel.message.fetch(args[0]) but that won't do what you'll expect it to do. This will actual assign the entire Promise to the variable. What I think you want to do is just use the respone from the Promise directly in the .then method.
So taking all that, please look at the snippet of code below, with inspiration taken from the MessageManager .fetch examples. Give it a try and see if it works.
// We no longer need to store the value of the fetch in a variable since that won't work as you expect it would.
// Also we're now fetching the message from the MESSAGE_DATABASE channel.
client.channels.cache.get(MESSAGE_DATABASE).fetch(args[0])
// The fetched message will be given as a parameter to the .then method.
.then(fetchedMessage => client.channels.cache.get(COMMAND_CHANNEL).send(fetchedMessage.content))
.catch(console.error);
The program should work in such a way that a channel named "members" will display the number of members on the server, but the program does not give errors and does not work itself.
Thanks in advance!
async def on_member_join(member):
guild = member.guild
channel = get(guild.channels, name = 'members')
await channel.edit(name = f'Учатники: {guild.member_count}')
#bot.event
async def on_member_remove(member):
guild = member.guild
channel = get(guild.channels, name = 'members')
await channel.edit(name = f'Учатники: {guild.member_count}')
I am not really sure if you have it in your program but just to be sure, define what is the attribute named "channel" so it knows what to edit, you can use get_channel to do it and put the channels ID afterward inside it ( https://discordpy.readthedocs.io/en/latest/api.html?highlight=get_channel#discord.Client.get_channel)
Maybe just try and use it the old way and use name = "Учатники: " + str(guild.member_count) (The member_count gives you an output of int so you may need to turn it into a string before displaying it.
(I have not tested anything and this answer is based on experience and reading documents and also you might want to take a look at https://discordpy.readthedocs.io/en/latest/api.html?highlight=member_count#discord.Guild.member_count)
I'm trying to create a playlist which the bot can send but I can't figure it out, here's the code I've tried although it only takes the text to trigger the command and I can't access the list anywhere.
Code:
#client.event
async def on_message(message):
if message.content == "=playlist":
playlist = message.content
await message.channel.send(playlist)
Any ideas?
I don't understand what the problem is, by the looks of it, when a users message is =playlist, your code will send all the contents of that message essentially back to them. Is that not what you want?