【问题标题】:how to make nickname channel? [duplicate]如何制作昵称频道? [复制]
【发布时间】: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

【问题讨论】:

  • ifelse 在同一缩进级别上存在一条线,这是不允许的。
  • 需要是因为它是定义用户输入的变量

标签: python discord


【解决方案1】:

试试看:

@bot.event
async def on_message(ctx):
  if ctx.channel.id != 853665391272263691:
    return
  # if ctx.channel.id == 853665391272263691 will continue
  text = ctx.message.content # before: ctx.message.content.lower()
  await ctx.author.edit(nick=text)

编辑

【讨论】:

  • 谢谢。我得到了这个..:`文件“main.py”,第23行,on_message text = message.content.lower() NameError: name 'message' is not defined`我认为这不起作用...
  • 我编辑了,现在可以了
  • 还没有。阅读此内容,然后说:stackoverflow.com/questions/64074333/…。我只有message has no attribute "message"
猜你喜欢
  • 1970-01-01
  • 2014-04-23
  • 2013-12-25
  • 2014-02-13
  • 2019-09-13
  • 1970-01-01
  • 2020-01-23
  • 2013-04-22
  • 1970-01-01
相关资源
最近更新 更多