简体   繁体   中英

How do i take a argument from the user in Python with a discord bot

例如,用户输入 !set_channel abc 我如何从客户端获取 abc 并将其设置为变量 我环顾四周但似乎无法在 python 中找到解决方案

Well if you know that the user has typed !set_channel , I'm assuming that means you have the whole message. If so, you can just use the split() method and take the second element, like so.

message = "!set_channel abc" # Replace with a function call that gets the discord message
argument = message.split(" ")[1] # Will get "abc".  We use 1 as the index because 0 is for the first element

The split method will take a string and "split" it into a bunch of other string by what is separated by the string passed in to the method. We passed in a space " " , and the message is !set_channel abc . So when we call message.split(" ") it returns ["!set_channel", "abc"] and when we use index 1 we get the "abc" part of it.

You can just use abc as an input from the discord command.

@client.command()
async def set_channel(ctx, variable):
    pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM