discord.py client.say #user only using ctx argument - python

Using:
#client.command(pass_context=True)
async def bla(ctx):
I want to add a await client.say that could # the user. I have been trying to use the (ctx.message.author)) but the I only got this printed in the Discord server: click here
Code used in image above: await client.say("Bla bla bla! #{}".format(ctx.message.author))
I don't want to add another argument to the async def bla(ctx):

Use the mention attribute of the author object.
#client.command(pass_context=True)
async def bla(ctx):
await client.say("Bla bla bla! {}".format(ctx.message.author.mention))

Related

discord py how to create function

how to create a function that gives a role and calls it.
I need a function that give a role and how to call it
I try to
async def test():
await bot.get_channel(870197219192614943).send('test')
and
asyncio.run(test())
but it's no working
Refer to the docs to learn how to use discord.py.
U need to use this:
#bot.command()
async def test(ctx):
await bot.get_channel(870197219192614943).send('Hello, World!')
Now if you send a test message with the bot prefix at the beginning to the guild chat, where there is a bot message, you will receive a response to the channel with this id
Just name the function differently if I understand you correctly
async def name_():
pass
async def name():
pass

Discord bot with discord.py

The context is that I want to make a discord bot with discord.py and when reading its documentation, it says that I can put attributes to the classes, but I don't know where to put them.
class discord.ext.commands.Command(func, **kwargs)
#discord.ext.commands.command(name=..., cls=..., **attrs)
If I put !test_1 hello world it returns only hello, but if I put !test_1 "hello world" it returns hello world.
the !test_2 command makes it so that there is no need to use quotes so !test_2 this is an example returns this is an example.
According to the documentation, with the rest_is_raw attribute I can make test_2 behave like test_1 and take only the first argument.
So my problem is that I don't know where to place the attribute.
My code:
`
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix= '!', intents=discord.Intents.all())
# Example command 1
#bot.command()
async def test_1(ctx, arg):
await ctx.send(arg)
# Example command 2
#bot.command()
async def test_2(ctx, *, arg):
await ctx.send(arg)
#Ping-pong
#bot.command()
async def ping(ctx):
await ctx.send('pong')
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="!help"))
print('My bot is ready')
bot.run('mytokenissecret')
`
I tried to understand the documentation, and I put the attribute where I thought it would work, but no attempt worked.
I searched for videos but none answered my problem.
Documentation really helps you to find out what's argument used for your command. In your case test_1(ctx, arg) and test_2(ctx, *, arg) are completely different. Because, in test_2 you passing whatever you type and send in Discord. Using * make you passing all argument. Normally if there's no *, 1 argument only can accept 1 word. By using *, it can accept all of the words/sentence that you send in

How do i put more than one command leading to the same response in discord.py?

#client.event
async def on_message(message):
if message.content == "fase":
channel = (mychannel)
await message.channel.send("Fase 1")
I'm trying to make the message.content detect multiple words and send the same message.channel.send
I tried
if message.content.lower() == "fase", "estagio", "missao", "sala":
if message.content.lower() == ("fase", "estagio", "missao", "sala"):
if message.content.lower() == "fase" or "estagio" or "missao" or "sala":
if message.content.lower() == ("fase" or "estagio" or "missao" or "sala"):
I read this post: How do I allow for multiple possible responses in a discord.py command?
That is the same exact problem but in his case it was the CaseSensitiveProblem that i already fixed in my code
And the second code for multiple words was:
bot = commands.Bot(command_prefix='!', case_insensitive=True)
#bot.command(aliases=['info', 'stats', 'status'])
async def about(self):
# your code here
And i did it and got a lot of errors that made the bot not even run (im using PyCharm with discord.py 1.4.1 and python 3.6):
#import and token things up here
bot = commands.Bot(command_prefix='i.')
#bot.command(aliases=['fase', 'estagio', 'missao', 'sala']) #'#' or 'def' expected
async def flame(self): #Unexpected indent // Unresolved reference 'self'
if message.content(self): #Unresolved reference 'message'
await message.send("Fase 1") #Unresolved reference 'message' // Statement expected, found Py:DEDENT
What can i do to fix it?
Here's how to use the Commands extension:
from discord.ext import commands
bot = commands.Bot(command_prefix='!', case_insensitive=True)
#bot.command(aliases=['info', 'stats', 'status'])
async def about(ctx):
#Your code here
Every commands have the following in common:
They are created using the bot.command() decorator.
By default, the command name is the function name.
The decorator and the function definition must have the same indentation level.
ctx (the fist argument) will be a discord.Context object, which contains a lot of informations (message author, channel, and content, discord server, the command used, the aliase the command was invoked with, ...)
Then, ctx allows you to use some shortcuts:
message.channel.send() becomes ctx.send()
message.author becomes ctx.author
message.channel becomes ctx.channel
Command arguments are also easier to use:
from discord import Member
from discord.ext import commands
bot = commands.Bot(command_prefix='!', case_insensitive=True)
#Command call example: !hello #Mr_Spaar
#Discord.py will transform the mention to a discord.Member object
#bot.command()
async def hello(ctx, member: Member):
await ctx.send(f'{ctx.author.mention} says hello to {member.mention}')
#Command call example: !announce A new version of my bot is available!
#"content" will contain everything after !announce (as a single string)
#bot.command()
async def announce(ctx, *, content):
await ctx.send(f'Announce from {ctx.author.mention}: \n{content}')
#Command call example: !sum 1 2 3 4 5 6
#"numbers" will contain everything after !sum (as a list of strings)
#bot.command()
async def sum(ctx, *numbers):
numbers = [int(x) for x in numbers]
await ctx.send(f'Result: {sum(numbers)}')

How do I resend user input after a command (e.g. User: !send 1234 Bot: 1234) in discord.py?

I have recently started programming my own discord bot as many other people are doing...
So far what I have got is this:
#bot.command
async def on_message(message):
if message.content.startswith("t!send"):
await client.send_message(message.content)
It doesn't crash but it doesn't work either...
It seems as if you're using a tutorial for an old version of discord.py.
There are some big changes in the most recent version - rewrite. Please take some time to look for more updated tutorials, or read the documentation linked above.
Here are the cases for using both the on_message event and commands:
#bot.command()
async def send(ctx, *, sentence):
await ctx.send(sentence)
######################################
#bot.event
async def on_message(message):
args = message.content.split(" ")[1:]
if message.content.startswith("t!send"):
await message.channel.send(" ".join(args))
else:
await bot.process_commands(message) # only add this if you're also using command decorators
References:
commands.Context() - if not familiar with using command decorators
discord.on_message()
Bot.process_commands()
So first of all, on_message doesn't belong here and you also don't have to use it. (on_message() would use the decorator #bot.event) Assuming you have setup a prefix for your bot using bot = commands.Bot(command_prefix = 't!') you could do something like this:
#bot.command()
async def send(ctx, *args):
message = " ".join(args)
await ctx.send(message)
*args is everything the user types in after t!send. So for example: if the user types in t!send Hello world! args would be ["Hello", "world!"]. With .join we can join them together into a single string (message)

How to receive and send message to specific channel in Discord.py?

Yesterday I was working on something simple where a bot on command of !name Barty would print back Hello Barty
#bot.command()
async def name(ctx, args):
await ctx.send("hello {}".format(args)
However the problem I am facing at this moment is that the bot would response to any channel where I do use the !name XXXX and what I am trying to do is that I want only to react to given specific channel in discord.
I tried to do:
#bot.event
async def on_message(message):
if message.channel.id == 1234567:
#bot.command()
async def name(ctx, args):
await ctx.send("hello {}".format(args)
but that was completely not working and I am out of ideas and here I am.
How can I send command to a given specific channel and get back response from there?
Move the define code out of the IF statement:
#bot.command()
async def name(ctx, args):
await ctx.send("hello {}".format(args)
When you've done that, you should be able to do;
if (message.channel.id == 'channel id'):
await message.channel.send('message goes here')
else:
# handle your else here, such as null, or log it to ur terminal
Could also check out the docs: https://discordpy.readthedocs.io
Once you make the command, make the IF statement inside of that.
message.channel.id now needs to be an integer, not a string so you don't need to use ''
if (message.channel.id == 1234567):
await message.channel.send('message goes here')
else:
# handle your else here, such as null, or log in to ur terminal

Categories

Resources