【发布时间】:2021-06-28 21:23:14
【问题描述】:
理论上我知道如何通过定义的命令更改昵称。这次我只想在我的服务器中创建一个特殊的频道,它将所有文本转换为用户昵称而不输入任何命令/前缀。 (并删除此文本)。我尝试了几种方法,但都失败了。一些例子:
@bot.event
async def on_message(ctx):
if ctx.channel.id != 853665391272263691:
return
text = message.content.lower()
else:
ctx.author.edit(nick={text})
抛出的错误:
File "main.py", line 23
else:
^
SyntaxError: invalid syntax
第二种方法:
@bot.event
async def bot.on_message(msg):
if msg.channel.id == 853665391272263691:
await msg.author.edit(nick=f'{msg.author.name}1')
else:
return
这引发了许多不同的错误,例如
msg undefined 或者只是没有权限(当这个机器人是管理员时。如何?)
经过几次尝试,我尝试了。我想我已经接近了,但它还没有工作......
@bot.event
async def on_message(ctx):
if ctx.channel.id != 853665391272263691:
return
# if ctx.channel.id == 853665391272263691 will continue
text = str(ctx.content).lower()
ctx.author.edit(nick=text)
抛出错误:
main.py:24: RuntimeWarning: coroutine 'Member.edit' was never awaited
ctx.author.edit(nick=text)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
导入的库:
import os
from datetime import datetime
import discord
from discord import Embed, Color
from discord.ext import commands
from keep_alive import keep_alive
from discord_slash import SlashCommand
没有办法让这种魅力发挥作用,或者......?
完整源代码here
【问题讨论】:
-
if和else在同一缩进级别上存在一条线,这是不允许的。 -
需要是因为它是定义用户输入的变量