![](/img/trans.png)
[英]How do I take input from the python console to be able to send messages as a Discord BOT?
[英]How do i take a argument from the user in Python with a discord bot
例如,用戶輸入 !set_channel abc 我如何從客戶端獲取 abc 並將其設置為變量 我環顧四周但似乎無法在 python 中找到解決方案
好吧,如果您知道用戶輸入了!set_channel
,我假設這意味着您擁有完整的消息。 如果是這樣,您可以使用split()
方法並獲取第二個元素,就像這樣。
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
split 方法將獲取一個字符串並通過傳遞給該方法的字符串分隔的內容將其“拆分”為一堆其他字符串。 我們傳入了一個空格" "
,消息是!set_channel abc
。 因此,當我們調用message.split(" ")
它返回["!set_channel", "abc"]
並且當我們使用索引 1 時,我們得到它的 "abc" 部分。
您可以使用 abc 作為 discord 命令的輸入。
@client.command()
async def set_channel(ctx, variable):
pass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.