Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python 3.x - Discord.py bot: how would I make my discord bot send me responses to a command that users use in DMs e.g. for a survey?

So, I want to make my discord bot have a command for a survey where if the user says something like "#survey" then the bot will DM them with the question. Then I want to make it so the response (where the user uses a command in DM to respond) will be sent to me via DM by the bot? Is this possible?

I know how to make the bot DM the user when they use the command in the discord server, but its the sending me the response part I can't get my head around.

I'm new to discord.py, but I have scanned through the documentation before asking this to check if i could find anything relevant.

Also this is my first ever question on this website, I just signed up so excuse this if it isn't written very well.

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I assume the part you're having trouble with is capturing the response. discord.py overloads the function(args, *, kwargs) syntax for commands, so that a single argument after the * is the text of the rest of the message.

from discord.ext.commands import Bot

bot = Bot('#')

my_user_id = "<Your ID>"  
# You can get your id through the discord client, after activating developer mode.

@bot.command(pass_context=True)
async def survey(ctx):
    await bot.send_message(ctx.message.author, "What's your name?")

@bot.command(pass_context=True)
async def respond(ctx, *, response):
    owner = await bot.get_user_info(my_user_id)
    await bot.send_message(owner, "{} responded: {}".format(ctx.message.author.name, response))

bot.run("token")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...