我已经开始为我的机器人建立一个升级系统,并且一直在努力尝试实现升级消息。但是,由于我正在创建的功能不是命令,因此我认为无法将上下文传递给它,这会导致问题。
由于缺乏更好的解决方案,我将其扔进了我的函数中,该函数检查exp并与某个级别匹配-发生的情况是制作了用户级别的副本,然后与更新的级别进行比较,以查看是否有级别发生了。此方法有效,但是我唯一的问题是无法发送“升级”消息。
async def levelcheck(user):
global userlvl
global userlvlc
channel = discord.Object(id="")
# copying the userlevel BEFORE update
try:
userlvlc = userlvl
except NameError as e:
print(e)
with open('userexp.json', 'r') as fp:
userexp = json.load(fp)
# finding out level based on exp
for x in range(0,len(levelcaps)):
if userexp[str(user)] <= levelcaps[x]:
userlvl = x
try:
if userlvlc < userlvl:
await ctx.send('You levelled up --> {}'.format(userlvl))
except NameError as e:
print(e)
#
return userlvl
您可以清楚地看到,这不是一个漂亮的命令,但肯定可以。我首先尝试获取频道ID,但后来才意识到我不知道如何使用上下文获取当前频道。有没有一种方法可以在没有上下文的情况下将消息发送到当前频道,还是我需要重新考虑我的方法?
答案 0 :(得分:1)
看到您有用户ID时,我会提出此解决方案。
User
object。User
object to get a DM Channel DM_channel
to send a level up message user_to_level_up = bot.fetch_user(user) # since your user variable is an ID
dm_channel = user_to_level_up.dm_channel
if dm_channel is None:
await user_to_level_up.create_dm()
dm_channel = user_to_level_up.dm_channel
await dm_channel.send("You've leveled up! You are now level {}".format(userlvl))
注意:这未经测试,但是链接应提供使它正常工作的信息!
这只是向他们发送个人消息,而不是在服务器上发布。要发布到服务器中,您似乎需要交叉引用该用户所在的服务器,因为该机器人也位于该服务器中,这对我来说XD听起来工作量太大