'else cant read the variable "msg"
so basically I have some code that will look at a Reddit page and get the title, link, current upvotes, and how long ago it was posted. And if the links aren't the same it will send an embed to my discord, but if they are the same then it won't do anything but i want to edit the embed so it changes the current upvotes but in the else function it can't read the variable "msg".
I've thought to use Global but I cant make it work, can anyone help?
if geta != oglink:
print('new link')
with open('file.txt', 'r+') as f:
f.write(f"{geta}")
f.close()
print(oglink)
print(geta)
print(gettime.text)
print(gettext.text)
#global msg
msg = await channel.send(embed=Newsembed)
with open('file.txt', 'r+') as f:
oglink = f.read()
f.close()
print("New Top reddit has been sent!")
else:
print(oglink)
print(geta)
print('Top reddit has not changed')
await channel2.send('top reddit has not changed')
global msg
await msg.edit(embed=editembed)
Solution 1:[1]
Put global in the declaration on the if statement, that way the variable msg will become global, like this:
if geta != oglink:
print('new link')
with open('file.txt', 'r+') as f:
f.write(f"{geta}")
f.close()
print(oglink)
print(geta)
print(gettime.text)
print(gettext.text)
#global msg
global msg = await channel.send(embed=Newsembed)
with open('file.txt', 'r+') as f:
oglink = f.read()
f.close()
print("New Top reddit has been sent!")
else:
print(oglink)
print(geta)
print('Top reddit has not changed')
await channel2.send('top reddit has not changed')
await msg.edit(embed=editembed)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | its.ilariiiiia |
